package com.example.pclimb.ui.adapters;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.pclimb.R;
import com.example.pclimb.model.GuideImage;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 证书图片适配器
 */
public class CertificateImageAdapter extends RecyclerView.Adapter<CertificateImageAdapter.ImageViewHolder> {

    private List<GuideImage> images = new ArrayList<>();
    private Context context;
    private OnImageClickListener onImageClickListener;

    public CertificateImageAdapter(Context context) {
        this.context = context;
    }

    @NonNull
    @Override
    public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_certificate_image, parent, false);
        return new ImageViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull ImageViewHolder holder, int position) {
        GuideImage image = images.get(position);
        
        // 加载图片 - 优先使用本地路径，如果没有则使用远程URL
        if (image.getImagePath() != null && !image.getImagePath().isEmpty()) {
            Glide.with(context)
                    .load(new File(image.getImagePath()))
                    .placeholder(R.drawable.placeholder_image)
                    .error(R.drawable.error_image)
                    .centerCrop()
                    .into(holder.imageView);
        } else if (image.getImageUrl() != null && !image.getImageUrl().isEmpty()) {
            Glide.with(context)
                    .load(image.getImageUrl())
                    .placeholder(R.drawable.placeholder_image)
                    .error(R.drawable.error_image)
                    .centerCrop()
                    .into(holder.imageView);
        }

        // 设置删除按钮点击事件
        holder.btnDelete.setOnClickListener(v -> {
            if (onImageClickListener != null) {
                onImageClickListener.onDeleteClick(image, position);
            }
        });

        // 设置图片点击事件
        holder.imageView.setOnClickListener(v -> {
            if (onImageClickListener != null) {
                onImageClickListener.onImageClick(image, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return images.size();
    }

    public void setImages(List<GuideImage> images) {
        this.images = images;
        notifyDataSetChanged();
    }

    public void addImage(GuideImage image) {
        this.images.add(image);
        notifyItemInserted(images.size() - 1);
    }

    public void removeImage(int position) {
        if (position >= 0 && position < images.size()) {
            images.remove(position);
            notifyItemRemoved(position);
        }
    }

    public GuideImage getImageAt(int position) {
        if (position >= 0 && position < images.size()) {
            return images.get(position);
        }
        return null;
    }

    /**
     * 图片点击监听器接口
     */
    public interface OnImageClickListener {
        void onImageClick(GuideImage image, int position);
        void onDeleteClick(GuideImage image, int position);
    }

    public void setOnImageClickListener(OnImageClickListener listener) {
        this.onImageClickListener = listener;
    }

    /**
     * 图片ViewHolder
     */
    static class ImageViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        ImageView btnDelete;

        ImageViewHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.img_certificate);
            btnDelete = itemView.findViewById(R.id.btn_delete_image);
        }
    }
} 